package dk.itu.spvc.tourtracker.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import dk.itu.spvc.tourtracker.model.Client;
import dk.itu.spvc.tourtracker.model.ResponseListener;

public class ServiceRemoteSQL {

	public static ServiceRemoteSQL service;
	private static final String SERVER_URL = "http://89.117.25.29:8080/~thespy/pervasive/rest.php";

	public ServiceRemoteSQL() {
	}

	public static ServiceRemoteSQL getService() {
		if (service != null)
			return service;
		else
			return new ServiceRemoteSQL();
	}

	public void uploadTrip(String json, ResponseListener listener) {

		try {
			HttpPost httpost = new HttpPost(SERVER_URL + "?action=upload_trip");

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("json", json));

			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			Client.sendRequest(httpost, listener);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	public void getTrips(ResponseListener listener) {

		HttpPost httpost = new HttpPost(SERVER_URL + "?action=get_trips");
		Client.sendRequest(httpost, listener);		
	}

	public void downloadTrip(int tripId, ResponseListener listener) {
		HttpPost httpost = new HttpPost(SERVER_URL + "?action=get_trip_data");
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("trip_id", tripId+""));

		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		Client.sendRequest(httpost, listener);
	}
}
